/*
 * Sort.java
 *
 * Created on 20 aprile 2004, 23.23
 */

package net.ermannofranco.xml.style;

import net.ermannofranco.xml.Tag;

/**
 * 
 * @author ermanno (ermanno.franco@gmail.com)
 */
public class Sort extends StyleTag {

	private static final long serialVersionUID = 1529161305243245599L;

	public enum SortType {
		TEXT, NUMBER
	}

	public enum SortOrder {
		ASC, DESC
	}

	// public static final int TEXT = 0;
	// public static final int NUMBER = 1;
	// public static final int ASCENDING = 0;
	// public static final int DESCENDING = 1;

	/**
	 * Creates a new instance of Sort Protetto
	 * 
	 * @param level
	 *            contatore di livello per indentazione, gestito automaticamente
	 * @param container
	 *            tag contenitore, automatico
	 */
	protected Sort(int level, Tag container) {
		super("sort", level, container);
	}

	/**
	 * @param select
	 * @return autoref
	 */
	public Sort setSelect(String select) {
		addAttr("select", select);
		return this;
	}

	/**
	 * @param datatype
	 * @return autoref
	 */
	public Sort setDatatype(SortType datatype) {
		addAttr("data-type", getDatatype(datatype));
		return this;
	}

	/**
	 * @param orderType
	 * @return autoref
	 */
	public Sort setOrder(SortOrder orderType) {
		addAttr("order", getOrder(orderType));
		return this;
	}

	private String getDatatype(SortType datatype) {
		switch (datatype) {
		case TEXT:
			return "text";
		case NUMBER:
			return "number";
		default:
			throw new StyleException("Tipo di ordinamento inesistente");
		}
	}

	private String getOrder(SortOrder orderType) {
		switch (orderType) {
		case ASC:
			return "ascending";
		case DESC:
			return "descending";
		default:
			throw new StyleException("Tipo di ordinamento inesistente");
		}

	}
}
